% This file, equations.sty (July 1990), contains a collection of macros
% to aid in constructing displayed equations in LaTeX.  Written by
% 
%  Charles Karney	      
%  Plasma Physics Laboratory	  E-mail:  Karney@Princeton.EDU
%  Princeton University		  Phone:   +1 609 243 2607
%  Princeton, NJ 08543-0451	  FAX:	   +1 609 243 2662
% with some ideas and macros borrowed from John Hobby and Stephen Gildea.

\def\fileversion{2.0}
\def\filedate{93/08/30}
\def\docdate {93/08/30}

%
% \changes{v2.0}{93/08/30}{Added optional arguments to \yesnumber and
%	\begin{equation}}
%

% --------------------USER DOCUMENTATION--------------------

% Use this as a style option, e.g.,
%     \documentstyle[equations]{article}
% These probably don't work in conjunction with the leqno option.

% This implements the following:

% (1) \yesnumber turns on an equation number in an eqnarray* environment
% (just as \nonumber turns it off in an eqnarray environment).  E.g.,
%     \begin{eqnarray*}
%        a &=& b \\
%          & & + c \\
%          & & + d \\
%          & & + e \\
%          & & + f \\
%          & & + g \yesnumber
%     \end{eqnarray*}
%
%   \yesnumber takes an optional argument that will be used for the
%   equation number.  The equation counter will not be incremented.
%	\begin{eqnarray*}
%          & & + f \\
%          & & + g \yesnumber[**]
%     \end{eqnarray*}
%

% (2) The eqalign environment is just like Plain TeX's \eqalign.  E.g.,
%     \begin{equation}
%           \begin{eqalign}
%              a &= b, \\
%              c &= d.
%           \end{eqalign}
%     \end{equation}
% Note that \begin{equation} \end{equation} or equivalent is needed.

% (3) The eqalignno environment is just like Plain TeX's \eqalignno.  E.g.,
%     \begin{eqalignno}
%        a &= b, \label{foo}\\
%        c &= d. \label{bar}
%     \end{eqalignno}
% Note the absence of \begin{equation} \end{equation}.  \nonumber can be used
% to suppress the equation number.  eqalignno* is the same except that the
% equation numbers are suppressed (unless a \yesnumber appears).

% (4) The eqaligntwo environment is a two-equation per line equivalent of
% eqalignno.  E.g.,
%     \begin{eqaligntwo}
%        a &= b, & x &= y, \label{foo} \\
%        c &= d, & z &= w. \label{bar}
%     \end{eqaligntwo}
% eqaligntwo* is defined similarly.

% (5) The cases environment is just like Plain TeX's \cases.  E.g.,
%     \begin{equation}
%        u(x) =
%           \begin{cases}
%             0, & for $x < 0$, \\
%             1, & for $x \ge 0$.
%           \end{cases}
%     \end{equation}
% Note the first column is treated as math, the second column as text.

% (6) I've borrowed John Hobby's modifications to the eqnarray environment
% (to fix up the spacing around the operator), and to the \big, \bigg, etc.
% operators (to make them scale with the point size).

% (7) I've borrowed Stephen Gildea's subequations environment, and fixed it
% so that it ignores spaces after the environment and so that you can refer
% both to the overall set of equations and to individual subequations.  E.g.,
%     \begin{subequations} \label{foo}
%       \begin{eqalignno}
%          a &= b, \label{foo-a} \\
%          c &= d, \label{foo-b}
%       \end{eqalignno}
%       text text text text ...
%       \begin{equation}
%          e = f. \label{foo-c}
%       \end{equation}
%     \end{subequations}
% Now \ref{foo}, \ref{foo-a}, \ref{foo-b}, \ref{foo-c}, produce 1, 1a, 1b,
% 1c.

% (8) The equation number is always printed in text mode.  Normally an
% equation environment would print it in math mode (with the \rm family).
% This only makes a difference if \rm in text and math modes gives you
% different fonts.
%
% (8) \begin{equation} can take an optional argument that will be used for the
% equation number.  The equation counter will not be incremented.
%	\begin{equation}[$G'$]
%		2 + 2 = 4
%	\end{equation}

% --------------------END OF USER DOCUMENTATION--------------------

% Fix up eqnarray* so that \yesnumber and \nonumber do the obvious things 
% Incorporate fixes of Peter Vanroose (peter@dit.lth.se) to make \yesnumber
% actually work.
\newif\if@defeqnsw \@defeqnswtrue

% Has the user specified a hard-wired equation number?
\newif\if@hardeqn \@hardeqnfalse

% This is John Hobby's (hobby@research.att.com) version to fix up the spacing.
\def\eqnarray{\stepcounter{equation}\let\@currentlabel=\theequation
\if@defeqnsw\global\@eqnswtrue\else\global\@eqnswfalse\fi
\tabskip\@centering\let\\=\@eqncr
$$\halign to \displaywidth\bgroup\hfil\global\@eqcnt\z@
  $\displaystyle\tabskip\z@{##}$&\global\@eqcnt\@ne 
  \hfil$\displaystyle{{}##{}}$\hfil
  &\global\@eqcnt\tw@ $\displaystyle{##}$\hfil 
  \tabskip\@centering&\llap{##}\tabskip\z@\cr}

\@namedef{eqnarray*}{\@defeqnswfalse\global\@eqnswfalse\eqnarray}
\@namedef{endeqnarray*}{\endeqnarray}

\def\yesnumber{\@ifnextchar[{\@yesnumber}{\global\@eqnswtrue}}	% ]

% Set up to use an hardwired equation number in an eqnarray
\def\@yesnumber[#1]{\global\@eqnswtrue
	\global\@hardeqntrue\let\ref=\expandableref\xdef\@hardeqndef{#1}}

% This is taken out of \@@eqncr because TeX \if nesting was misdesigned - ews
\def\make@eqnnum{\if@hardeqn{\def\theequation{\@hardeqndef}\@eqnnum}\global\@hardeqnfalse\else
                                 \@eqnnum\stepcounter{equation}\fi}

\def\@@eqncr{\let\@tempa\relax \global\advance\@eqcnt by \@ne
    \ifcase\@eqcnt \def\@tempa{& & & &}\or \def\@tempa{& & &}\or
     \def\@tempa{& &}\or \def\@tempa{&}\else\fi 
     \@tempa \if@eqnsw\make@eqnnum\fi
     \if@defeqnsw\global\@eqnswtrue\else\global\@eqnswfalse\fi
     \global\@eqcnt\z@\cr}

% Several formulas like \eqalign (to go inside \begin{equation}
% \end{equation} or $$ $$.

\def\@eqnacr{{\ifnum0=`}\fi\@ifstar{\@yeqnacr}{\@yeqnacr}}

\def\@yeqnacr{\@ifnextchar [{\@xeqnacr}{\@xeqnacr[\z@]}}

\def\@xeqnacr[#1]{\ifnum0=`{\fi}\cr \noalign{\vskip\jot\vskip #1\relax}}

\def\eqalign{\null\,\vcenter\bgroup\openup1\jot \m@th \let\\=\@eqnacr
\ialign\bgroup\strut
\hfil$\displaystyle{##}$&$\displaystyle{{}##}$\hfil\crcr}
\def\endeqalign{\crcr\egroup\egroup\,}

% \cases

\def\cases{\left\{\,\vcenter\bgroup\normalbaselines\m@th \let\\=\@eqnacr
    \ialign\bgroup$##\hfil$&\quad##\hfil\crcr}
\def\endcases{\crcr\egroup\egroup\right.}

% Several formulas like \eqalignno (doesn't need the $$ $$)

\def\eqalignno{\stepcounter{equation}\let\@currentlabel=\theequation
\if@defeqnsw\global\@eqnswtrue\else\global\@eqnswfalse\fi
\let\\=\@eqncr
$$\displ@@ \tabskip\@centering \halign to \displaywidth\bgroup
  \global\@eqcnt\@ne\hfil
  $\@lign\displaystyle{##}$\tabskip\z@skip&\global\@eqcnt\tw@
  $\@lign\displaystyle{{}##}$\hfil\tabskip\@centering&
  \llap{\@lign##}\tabskip\z@skip\crcr}

\def\endeqalignno{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$\global\@ignoretrue}

\def\displ@@{\global\dt@ptrue\openup\jot\m@th % Remove \penalty from \displ@y
  \everycr{\noalign{\ifdt@p \global\dt@pfalse
      \vskip-\lineskiplimit \vskip\normallineskiplimit \fi}}}

% Here's the eqalignno* environment:

\@namedef{eqalignno*}{\@defeqnswfalse\eqalignno}
\@namedef{endeqalignno*}{\endeqalignno}

% Two formulas like \eqalignno (doesn't need the $$ $$)

\def\eqaligntwo{\stepcounter{equation}\let\@currentlabel=\theequation
\if@defeqnsw\global\@eqnswtrue\else\global\@eqnswfalse\fi
\let\\=\@eqncr
$$\displ@@ \tabskip\@centering \halign to \displaywidth\bgroup
  \global\@eqcnt\m@ne\hfil
  $\@lign\displaystyle{##}$\tabskip\z@skip&\global\@eqcnt\z@
  $\@lign\displaystyle{{}##}$\hfil\qquad&\global\@eqcnt\@ne
  \hfil$\@lign\displaystyle{##}$&\global\@eqcnt\tw@
  $\@lign\displaystyle{{}##}$\hfil\tabskip\@centering&
  \llap{\@lign##}\tabskip\z@skip\crcr}

\def\endeqaligntwo{\@@eqncr\egroup
      \global\advance\c@equation\m@ne$$\global\@ignoretrue}

\@namedef{eqaligntwo*}{\@defeqnswfalse\eqaligntwo}
\@namedef{endeqaligntwo*}{\endeqaligntwo}

% subequations
%%% File: subeqn.sty
%%% The subequations environment %%%
%
% Within the subequations environment, the only change is that
% equations are labeled differently.  The number stays the same,
% and lower case letters are appended.  For example, if after doing
% three equations, numbered 1, 2, and 3, you start a subequations
% environmment and do three more equations, they will be numbered
% 4a, 4b, and 4c.  After you end the subequations environment, the
% next equation will be numbered 5.
%
% Both text and equations can be put inside the subequations environment.
%
% If you make any improvements, I'd like to hear about them.
%
% Stephen Gildea
% MIT Earth Resources Lab
% Cambridge, Mass. 02139
% mit-erl!gildea
% gildea@erl.mit.edu
%
\newtoks\@stequation

\def\subequations{\refstepcounter{equation}%
  \edef\@savedequation{\the\c@equation}%
  \@stequation=\expandafter{\theequation}%   %only want \theequation
  \edef\@savedtheequation{\the\@stequation}% %expanded once
  \edef\oldtheequation{\theequation}%
  \setcounter{equation}{0}%
  \def\theequation{\oldtheequation\alph{equation}}}

\def\endsubequations{%
  \ifnum\c@equation < 2 \@warning{Only \the\c@equation\space subequation
    used in equation \@savedequation}\fi
  \setcounter{equation}{\@savedequation}%
  \@stequation=\expandafter{\@savedtheequation}%
  \edef\theequation{\the\@stequation}%
  \global\@ignoretrue}

% More from John Hobby
% Fix Plain's \bigl, \Bigl, etc. macros so that they try to scale with
% LaTeX size changes.

\ifx\selectfont\undefined	% nonNFSS version

% This uses the fact that \@setsize sets \ht\strutbox
% to be 70% of the normal unstretched baselineskip.
% (Actually it doesn't, so we have to redefine \@setsize to define \strutboxa

\newbox\strutboxa
\def\@setsize#1#2#3#4{\@nomath#1\let\@currsize#1\baselineskip
   #2\setbox\strutboxa\hbox{\vrule height.7\baselineskip
      depth.3\baselineskip width\z@}\baselineskip\baselinestretch\baselineskip
   \normalbaselineskip\baselineskip#3#4}
\def\struta{\relax\ifmmode\copy\strutboxa\else\unhcopy\strutboxa\fi}

\def\big#1{{\hbox{$\left#1\vcenter to1.428\ht\strutboxa{}\right.\n@space$}}}
\def\Big#1{{\hbox{$\left#1\vcenter to2.142\ht\strutboxa{}\right.\n@space$}}}
\def\bigg#1{{\hbox{$\left#1\vcenter to2.857\ht\strutboxa{}\right.\n@space$}}}
\def\Bigg#1{{\hbox{$\left#1\vcenter to3.571\ht\strutboxa{}\right.\n@space$}}}

\else % NFSS version

% Change \fontsize so that it remembers the \baselineskip in \baselinex
\newdimen\baselinex
\newbox\strutboxa
\def\fontsize#1#2{%
    \edef\f@size{#1}%
    \edef\setnew@baselineskip{\baselinex #2\relax\showthe\baselinex
	\setbox\strutboxa\hbox{\vrule height.7\baselinex depth.3\baselinex width\z@}%
	\baselineskip #2\relax}%
    \edef\lcl@currsize{#1#2}}
\baselinex=\baselineskip
\def\struta{\relax\ifmmode\copy\strutboxa\else\unhcopy\strutboxa\fi}

\def\big#1{{\hbox{$\left#1\vcenter to1.0\baselinex{}\right.\n@space$}}}
\def\Big#1{{\hbox{$\left#1\vcenter to1.5\baselinex{}\right.\n@space$}}}
\def\bigg#1{{\hbox{$\left#1\vcenter to2.0\baselinex{}\right.\n@space$}}}
\def\Bigg#1{{\hbox{$\left#1\vcenter to2.5\baselinex{}\right.\n@space$}}}

\fi % nonNFSS version

% Finally, always put equation number in a box.  (Makes a difference when
% text Roman is not the same as textfont0

\def\@eqnnum{\mbox{\rm (\theequation)}}

% modify \begin{equation} \end{equation} so it will take an optional arg.
%
\def\equation{$$ % $$ BRACE MATCHING HACK
	\@ifnextchar[%     ] BRACE MATCHING HACK
	{\let\protect=\noexpand\@equation}%
	{\refstepcounter{equation}}}

\def\expandableref#1{\@ifundefined{r@#1}{??}%
        {\expandafter\expandafter\expandafter\@car\csname r@#1\endcsname\@nil}}

\let\@saveref=\ref
\def\@equation[#1]{%
	\let\ref=\expandableref
	\edef\theequation{#1}%
%\show\theequation
	\let\@currentlabel=\theequation
	\let\ref=\@saveref}

\endinput
